import { defineStore } from "pinia";
// import { ucenterLogin, mockRequest1 } from "@/api/mock";
import {
  dataMiddleGroundBaseUserLogin,
  dataMiddleGroundBaseUserLoginBySms,
} from "@/api/data-middleground-base/base/user";
import Encryption from "@/utils/base64encrytion.js";
import router from "@/router";
const loginStore = defineStore({
  id: "login",
  state: () => ({
    form: {
      mobile: "",
      password: "",
      autoLogin: false,
      code: "",
    },
  }),
  actions: {
    /**
     * 登录
     */
    async login() {
      const res = await dataMiddleGroundBaseUserLogin({
        mobile: this.form.mobile,
        password: this.form.password,
      });
      let path = router.currentRoute.value.path;

      if (res) {
        window.localStorage.setItem("token", res);
        if (path == "/login") {
          router.push({
            path: "/data-lake",
          });
        } else {
          window.location.reload();
        }
      }
    },
    //短信登录
    async codeLogin() {
      const res = await dataMiddleGroundBaseUserLoginBySms({
        mobile: this.form.mobile,
        password: this.form.code,
      });
    },
    async getInfos() {
      // const res = await mockRequest1(this.form);
      // console.log(res);
    },
    getLoginInfos() {
      try {
        const login = new Encryption().base64decode(
          window.localStorage.getItem("login") || "{}",
        );
        this.form = JSON.parse(login);
        this.form.code = "";
      } catch (error) {}
    },
    setUserInfos() {
      const login = new Encryption().base64encode(JSON.stringify(this.form));
      window.localStorage.setItem("login", login);
    },
  },
});

export default loginStore;
